package com.jinmdz.fmis.dao.admin.dao.provider;

import com.jinmdz.fmis.dao.base.BaseProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 字典费用关联及计算规则设置Dao
 *
 * @author GuanKui
 * @date 2020/3/12 13:07
 */
public class ServiceDictionaryProvider extends BaseProvider {

    /**
     * 根据dict_code获取字典费用关联及计算规则信息
     *
     * @param dictCode 查询编码
     * @return
     * @author GuanKui
     * @date 2020/3/9  15:35
     */
    public String listServiceDictionaryDataListByDictCode(@Param("dictCode") String dictCode) {
        return new SQL() {
            {

                StringBuilder builder = new StringBuilder();
                builder.append(" service_dictionary_item.id,service_dictionary_item.version");
                builder.append(" ,data_dictionary.id as data_dictionary_id,data_text as data_dictionary_text");
                builder.append(" ,service_item_id,item_name as service_item_name");
                builder.append(" ,purpose,rule_code,rule_type,rule_describe");
                SELECT(builder.toString());
                FROM(" data_dictionary ");
                LEFT_OUTER_JOIN(" service_dictionary_item on data_dictionary_id = data_dictionary.id and service_dictionary_item.deleted = 0 ");
                LEFT_OUTER_JOIN(" service_item on service_item.id = service_item_id and service_item.deleted = 0 ");
                WHERE(" data_dictionary.deleted = 0 and data_dictionary.disable_state = 0  ");
                WHERE(" data_dictionary.dict_code = #{dictCode}");
                ORDER_BY(" data_dictionary.sort ");
            }
        }.toString();
    }

    /**
     * 根据服务字典关联主键及字典主键获取字典费用关联及计算规则信息
     *
     * @param id               service_dictionary_item表ID
     * @param dataDictionaryId data_dictionary表查询ID
     * @return
     * @author GuanKui
     * @date 2020/3/9  15:35
     */
    public String getServiceDictionaryDataEditByDictionaryId(@Param("id") Integer id, @Param("dataDictionaryId") Integer dataDictionaryId) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" data_dictionary.id as data_dictionary_id,data_dictionary.data_text as data_dictionary_text");
                builder.append(" ,service_item.id as service_item_id,service_item.item_name as service_item_name,service_item.item_price as service_item_price");
                builder.append(" ,service_dictionary_item.id,service_dictionary_item.purpose,service_dictionary_item.rule_code,service_dictionary_item.rule_type,service_dictionary_item.rule_describe,service_dictionary_item.rule_params,service_dictionary_item.version");
                builder.append(" ,calculate_rule.rule_describe as calculate_rule_describe");
                SELECT(builder.toString());
                FROM(" data_dictionary ");
                LEFT_OUTER_JOIN(" service_dictionary_item on data_dictionary_id = #{dataDictionaryId} and service_dictionary_item.id = #{id} and service_dictionary_item.deleted = 0");
                LEFT_OUTER_JOIN(" service_item on service_item.id = service_dictionary_item.service_item_id and service_item.deleted = 0");
                LEFT_OUTER_JOIN(" calculate_rule on calculate_rule.rule_code = service_dictionary_item.rule_code and calculate_rule.deleted = 0");
                WHERE(" data_dictionary.id = #{dataDictionaryId} and data_dictionary.deleted = 0 and data_dictionary.disable_state = 0");
                ORDER_BY(" data_dictionary.id limit 1");
            }
        }.toString();
    }

    /**
     * 按照主键获取关联信息
     *
     * @param id 主键
     * @return
     * @author LiCongLu
     * @date 2020-03-28 15:14
     */
    public String getServiceDictionaryDataEditById(@Param("id") Integer id) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" service_dictionary_item.id,service_dictionary_item.purpose,service_dictionary_item.rule_code,service_dictionary_item.rule_type,service_dictionary_item.rule_describe,service_dictionary_item.rule_params,service_dictionary_item.version");
                builder.append(" ,calculate_rule.rule_describe as calculate_rule_describe");
                builder.append(" ,data_dictionary.id as data_dictionary_id,data_dictionary.data_text as data_dictionary_text");
                builder.append(" ,service_item.id as service_item_id,service_item.item_name as service_item_name,service_item.item_price as service_item_price");
                SELECT(builder.toString());
                FROM(" service_dictionary_item ");
                JOIN(" data_dictionary on data_dictionary.id = data_dictionary_id ");
                JOIN(" service_item on service_item.id = service_dictionary_item.service_item_id ");
                LEFT_OUTER_JOIN(" calculate_rule on calculate_rule.rule_code = service_dictionary_item.rule_code and calculate_rule.deleted = 0");
                WHERE(" service_dictionary_item.deleted = 0 and data_dictionary.deleted = 0 ");
                WHERE(" service_dictionary_item.id = #{id} ");
                ORDER_BY(" data_dictionary.id limit 1");
            }
        }.toString();
    }

    /**
     * 获取计算规则
     *
     * @param initialCode 业务类型规则编码开头
     * @return
     * @author LiCongLu
     * @date 2020-04-14 15:19
     */
    public String listServiceDictionaryDataRuleForCode(@Param("initialCode") String initialCode) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" id,rule_code,rule_type,rule_version,call_explanation,rule_describe,disable_state,remark,version ");
                SELECT(builder.toString());
                FROM(" calculate_rule ");
                WHERE(" deleted = 0 ");
                // 验证规则编码
                if (valid(initialCode)) {
                    WHERE(" rule_code like '" + initialCode + "%' ");
                }
                ORDER_BY(" calculate_rule.rule_code");
            }
        }.toString();
    }
}
